import React, { Component } from "react"
// 导入参数校验包
import propTypes from "prop-types"

class Mouse extends Component {
    // 鼠标状态
    state = {
        x: 0,
        y: 0,
        isShow: true
    }

    handMouseMove = e => {
        console.log(e);
        this.setState({
            x: e.clientX,
            y: e.clientY
        })
    }

    // 组件挂载完成之后调用
    componentDidMount() {
        window.addEventListener('mousemove', this.handMouseMove)
        console.log(this.props);
    }

    // 组件卸载
    componentWillUnmount() {
        // 组件卸载时将绑定事件清除
        window.removeEventListener('mousemove', this.handMouseMove)
        console.log("组件已卸载");
    }


    render() {
        if (!this.state.isShow) return null
        return this.props.children(this.state)
    }

}

Mouse.propTypes = {
    children: propTypes.func.isRequired
}

export default Mouse;